import $ from "jquery";
import {url} from "../../util/config";
import {message} from "antd";
import login from "../login";

function getNewLogin(type,flag) {
    return getLinks(type,flag);
}

export default function getLinks(type,flag) {
    let links = [];
    if(!localStorage.getItem("token")){
        return
    }
    let token = localStorage.getItem("token")
    $.ajax({
        async:false,
        headers: {
            'Accept': "application/json",
            'Authorization': token
        },
        url: url.api_url+`LinkManage/GetLinks?type=${type}`,
        type: 'GET',
        success: function (result) {
            let tmpLink = null;
            let checked = false;
            if(result.result=="Token過期請重新登錄") {
                login(localStorage.getItem("account"), localStorage.getItem("password"));
                links = getNewLogin(type,flag)
            }else {
                links = result.result.map(item=>{
                        if(item.LINK_TYPE=="link"){
                            item.LINK_TYPE= "链接"
                        }else if(item.LINK_TYPE=="slideshow") {
                            item.LINK_TYPE= "轮播图"
                        }else if(item.LINK_TYPE=="small"){
                            item.LINK_TYPE= "小图片"
                        }

                        if(flag=="side"){
                            tmpLink = item.LINK_URL
                        }else{
                            if(item.LINK_URL!=null){
                                if(item.LINK_URL.match(/<a/) ){
                                    if(item.LINK_URL.match(/(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|\:|\&|-)+)/g)!=undefined || item.LINK_URL.match(/(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|\&|-)+)/g)!=null){
                                        tmpLink = item.LINK_URL.match(/(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|\:|\&|-)+)/g)[0]
                                    }else {
                                        tmpLink = null
                                    }
                                }else {
                                    tmpLink = null
                                }

                                if(item.LINK_URL.match(/target="_blank"/)){
                                    checked=true
                                }else {
                                    checked=false
                                }
                            } else {
                                checked=false
                                tmpLink = null
                            }
                        }
                        return (
                            {
                                key:item.LINK_ID,
                                image_url:item.LINK_IMAGE_URL,
                                sortNum:item.SORT_NUM,
                                name:item.LINK_NAME,
                                type:item.LINK_TYPE,
                                link:tmpLink,
                                checked:checked,
                                createUser:item.CREATE_BY,
                                createTime:item.CREATE_DATE,
                                updateUser:item.UPDATE_BY,
                                updateTime:item.UPDATE_DATE
                            }
                        )
                    }
                )
            }

        },
        error:function (error) {
            message.error("请求异常")
        }
    })
    console.log("links",links)
    return links;
}
